<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<!-- Copyright 1997 The Open Group, All Rights Reserved -->
<title>sem_wait</title>
</head><body bgcolor=white>
<center>
<font size=2>
The Single UNIX &reg; Specification, Version 2<br>
Copyright &copy; 1997 The Open Group

</font></center><hr size=2 noshade>
<h4><a name = "tag_000_008_342">&nbsp;</a>NAME</h4><blockquote>
sem_wait, sem_trywait - lock a semaphore
(<b>REALTIME</b>)
</blockquote><h4><a name = "tag_000_008_343">&nbsp;</a>SYNOPSIS</h4><blockquote>
<pre><code>

#include &lt;<a href="semaphore.h.html">semaphore.h</a>&gt;

int sem_wait(sem_t *<i>sem</i>);
int sem_trywait(sem_t *<i>sem</i>);
</code>
</pre>
</blockquote><h4><a name = "tag_000_008_344">&nbsp;</a>DESCRIPTION</h4><blockquote>
The
<i>sem_wait()</i>
function locks the semaphore referenced by
<i>sem</i>
by performing a semaphore lock operation
on that semaphore.
If the semaphore value is currently zero,
then the calling thread 
will not return from the call to
<i>sem_wait()</i>
until it either locks the semaphore or the call is interrupted by a signal.
The
<i>sem_trywait()</i>
function locks the semaphore referenced by
<i>sem</i>
only if the semaphore is currently not locked;
that is, if the semaphore value is currently positive.
Otherwise, it does not lock the semaphore.
<p>
Upon successful return, the state of the semaphore is
locked and remains locked until the
<i><a href="sem_post.html">sem_post()</a></i>
function is executed and returns successfully.
<p>
The
<i>sem_wait()</i>
function is interruptible by the delivery of a signal.
</blockquote><h4><a name = "tag_000_008_345">&nbsp;</a>RETURN VALUE</h4><blockquote>
The
<i>sem_wait()</i>
and
<i>sem_trywait()</i>
functions return zero if
the calling process successfully performed the
semaphore lock operation
on the semaphore designated by
<i>sem</i>.
If the call was unsuccessful,
the state of the semaphore is unchanged, and
the function returns a value of -1 and sets
<i>errno</i>
to indicate the error.
</blockquote><h4><a name = "tag_000_008_346">&nbsp;</a>ERRORS</h4><blockquote>
The 
<i>sem_wait()</i>
and
<i>sem_trywait()</i>
functions will fail if:
<dl compact>

<dt>[EAGAIN]<dd>
The semaphore was already locked,
so it cannot be immediately locked by the
<i>sem_trywait()</i>
operation (
<a href="sem_trywait.html">sem_trywait</a>
only).

<dt>[EINVAL]<dd>
The
<i>sem</i>
argument does not refer to a valid semaphore.

<dt>[ENOSYS]<dd>
The functions
<i>sem_wait()</i>
and
<i>sem_trywait()</i>
are not supported by this implementation.

</dl>
<p>
The 
<i>sem_wait()</i>
and
<i>sem_trywait()</i>
functions may fail if:
<dl compact>

<dt>[EDEADLK]<dd>
A deadlock condition was detected.

<dt>[EINTR]<dd>
A signal interrupted this function.

</dl>
</blockquote><h4><a name = "tag_000_008_347">&nbsp;</a>EXAMPLES</h4><blockquote>
None.
</blockquote><h4><a name = "tag_000_008_348">&nbsp;</a>APPLICATION USAGE</h4><blockquote>
Realtime applications may encounter priority inversion when using
semaphores.
The problem occurs when a high priority thread &quot;locks&quot; (that is,
waits on) a semaphore that is about to be &quot;unlocked&quot; (that is, posted)
by a low priority thread, but the low priority thread is preempted by a medium
priority thread.
This scenario leads to priority inversion; a high priority thread is
blocked by lower priority threads for an unlimited period of time.
During system design, realtime programmers must take into account the
possibility of this kind of priority inversion.
They can deal with it in a number of ways, such as by having critical
sections that are guarded by semaphores execute at a high
priority, so that a thread cannot be preempted while executing in its
critical section.
</blockquote><h4><a name = "tag_000_008_349">&nbsp;</a>FUTURE DIRECTIONS</h4><blockquote>
None.
</blockquote><h4><a name = "tag_000_008_350">&nbsp;</a>SEE ALSO</h4><blockquote>
<i><a href="semctl.html">semctl()</a></i>,
<i><a href="semget.html">semget()</a></i>,
<i><a href="semop.html">semop()</a></i>,
<i><a href="sem_post.html">sem_post()</a></i>,
<i><a href="semaphore.h.html">&lt;semaphore.h&gt;</a></i>.
</blockquote><h4>DERIVATION</h4><blockquote>
Derived from the POSIX Realtime Extension (1003.1b-1993/1003.1i-1995)
</blockquote><hr size=2 noshade>
<center><font size=2>
UNIX &reg; is a registered Trademark of The Open Group.<br>
Copyright &copy; 1997 The Open Group
<br> [ <a href="../index.html">Main Index</a> | <a href="../xshix.html">XSH</a> | <a href="../xcuix.html">XCU</a> | <a href="../xbdix.html">XBD</a> | <a href="../cursesix.html">XCURSES</a> | <a href="../xnsix.html">XNS</a> ]

</font></center><hr size=2 noshade>
</body></html>

